const SQLITE3_ISO8859         =   1
const SQLITE3_OK              =   0   '*/ Successful result */
const SQLITE3_ERROR           =   1   '*/ SQL error or missing database */
const SQLITE3_INTERNAL        =   2   '*/ An internal logic error in SQLite */
const SQLITE3_PERM            =   3   '*/ Access permission denied */
const SQLITE3_ABORT           =   4   '*/ Callback routine requested an abort */
const SQLITE3_BUSY            =   5   '*/ The database file is locked */
const SQLITE3_LOCKED          =   6   '*/ A table in the database is locked */
const SQLITE3_NOMEM           =   7   '*/ A malloc() failed */
const SQLITE3_READONLY        =   8   '*/ Attempt to write a readonly database */
const SQLITE3_INTERRUPT       =   9   '*/ Operation terminated by sqlite3_interrupt() */
const SQLITE3_IOERR           =   10   '*/ Some kind of disk I/O error occurred */
const SQLITE3_CORRUPT         =   11   '*/ The database disk image is malformed */
const SQLITE3_NOTFOUND        =   12   '*/ (Internal Only) Table or record not found */
const SQLITE3_FULL            =   13   '*/ Insertion failed because database is full */
const SQLITE3_CANTOPEN        =   14   '*/ Unable to open the database file */
const SQLITE3_PROTOCOL        =   15   '*/ Database lock protocol error */
const SQLITE3_EMPTY           =   16   '*/ (Internal Only) Database table is empty */
const SQLITE3_SCHEMA          =   17   '*/ The database schema changed */
const SQLITE3_TOOBIG          =   18   '*/ Too much data for one row of a table */
const SQLITE3_CONSTRAINT      =   19   '*/ Abort due to contraint violation */
const SQLITE3_MISMATCH        =   20   '*/ Data type mismatch */
const SQLITE3_MISUSE          =   21   '*/ Library used incorrectly */
const SQLITE3_NOLFS           =   22   '*/ Uses OS features not supported on host */
const SQLITE3_AUTH            =   23   '*/ Authorization denied */
const SQLITE3_ROW             =   100  '*/ sqlite3_step() has another row ready */
const SQLITE3_DONE            =   101  '*/ sqlite3_step() has finished executing */

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'	FUNCTION DECLARATIONS
'	these declare the sqlite3 function calls in sqlite3.dll
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare function sqlite3_open lib "sqlite3.dll" alias "sqlite3_open" _
		( zFilename as string, hSQLite as long ) as long
declare sub sqlite3_close lib "sqlite3.dll" alias "sqlite3_close" _
		( byval hSQLite as long )

declare function sqlite3_last_insert_rowid lib "sqlite3.dll" alias "sqlite3_last_insert_rowid" _
		( byval hSQLite as long ) as long

declare function sqlite3_changes lib "sqlite3.dll" alias "sqlite3_changes" _
		( byval hSQLite as long ) as long
declare sub sqlite3_interrupt lib "sqlite3.dll" alias "sqlite3_interrupt" _
		( byval hSQLite as long )
declare function sqlite3_complete lib "sqlite3.dll" alias "sqlite3_complete" _
		( zSQL as string ) as long
declare function sqlite3_get_table lib "sqlite3.dll" alias "sqlite3_get_table" _
		( byval hSQLite as long, zSQL as string, lpTable as long, iRow as long, _
		  iCol as long, lpErrMsg as long ) as long
declare function sqlite3_free_table lib "sqlite3.dll" alias "sqlite3_free_table" _
		( byval lpTable as long ) as long

declare function sqlite3_prepare lib "sqlite3.dll" alias "sqlite3_prepare" _
		( byval hSQLite as long, zSQL as string, byval iLen as long, _
		  ppVMM as long, pzTail as long ) as long
declare function sqlite3_finalize lib "sqlite3.dll" alias "sqlite3_finalize" _
		( byval pStmt as long ) as long
declare function sqlite3_step lib "sqlite3.dll" alias "sqlite3_step" _
		( byval pStmt as long ) as long
declare function sqlite3_errcode lib "sqlite3.dll" alias "sqlite3_errcode" _
		( byval hSQLite as long ) as long
declare function sqlite3_errmsg lib "sqlite3.dll" alias "sqlite3_errmsg" _
		( byval hSQLite as long ) as long

declare function sqlite3_create_function lib "sqlite3.dll" alias "sqlite3_create_function" _
		( byval hSQLite as long, zName as string, byval nArg as long, _
		  byval eTextRep as long, byval pAny as long, byval pFunction as long, _
		  byval pStep as long, byval pFinal as long ) as long
declare function sqlite3_create_collation lib "sqlite3.dll" alias "sqlite3_create_collation" _
		( byval hSQLite as long, zName as string, byval pref16 as long, _
		  byval pAny as long, byval pCompare as long) as long

declare function sqlite3_column_count lib "sqlite3.dll" alias "sqlite3_column_count" _
		( byval hStmt as long ) as long
declare function sqlite3_column_name lib "sqlite3.dll" alias "sqlite3_column_name" _
		( byval hStmt as long, byval iCol as long ) as long
declare function sqlite3_column_text lib "sqlite3.dll" alias "sqlite3_column_text" _
		( byval hStmt as long, byval iCol as long ) as long

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'	sql3errCode
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function sql3errCode ( byval hSQLite as long ) as long

	result =sqlite3_errcode(hSQLite)

end function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' RQ functions - wrapped around sqlite3.dll functions
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'	sql3errMsg
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function sql3errMsg ( byval hSQLite as long ) as string

	dim pzErr as long

	pzErr = sqlite3_errmsg(hSQLite)
	if pzErr = 0 then
		result =""
	else
		result =varptr$(pzErr)
	end if

end function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'	sql3comm
' query<>"select..." - the result is the number of modified rows (or error number)
'                    - err_str is error string returned by sqlite3
' query="select..."  - the result is the number of rows returned by query
'                    - err_str is the same
'                    - ptable contains the result of the query
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function sql3comm(query as string,dbfile as string,byref err_str as string,byref ptable as qstringgrid) as long
dim hdb as long
dim stmt as long
dim rezultat as long
dim errstr as string
dim cols as integer
dim i as integer
dim currow as integer
 
rezultat=sqlite3_open(dbfile,varptr(hdb))
if rezultat<>0 then
 result=-rezultat
 errstr=sql3errMsg(hdb)
 err_str=insert$("open error: ",errstr,1)
 exit function
end if
rezultat=sqlite3_complete(query)
if rezultat<>0 then
 result=-rezultat
 errstr=sql3errMsg(hdb)
 err_str=insert$("query syntax error: ",errstr,0)
 exit function
end if
rezultat=sqlite3_prepare (hdb,query,len(query),varptr(stmt),varptr(errstr))
if rezultat<>SQLITE3_OK then
 result=-rezultat
 errstr=sql3errMsg(hdb)
 err_str=insert$("prepare error: ",errstr,0)
 exit function
end if
cols=sqlite3_column_count(stmt)
if lcase$(field$(query," ",1))<>"select" then

  rezultat=sqlite3_step(stmt)
  if rezultat= SQLITE3_ERROR or rezultat= SQLITE3_MISUSE then
   result=-rezultat
   errstr=sql3errMsg(hdb)
   err_str=insert$("step error: ",errstr,1)
   exit function
  end if
  while rezultat <>SQLITE3_DONE
   if rezultat= SQLITE3_ERROR or rezultat= SQLITE3_MISUSE then
    result=-rezultat
    errstr=sql3errMsg(hdb)
    err_str=insert$("step error: ",errstr,1)
    exit function
   end if
   rezultat=sqlite3_step(stmt)
  wend
  rezultat=sqlite3_finalize(stmt)
  if rezultat=SQLITE3_ERROR then
   result=-rezultat
   errstr=sql3errMsg(hdb)
   err_str=insert$("finalize error: ",errstr,1)
   exit function
  end if
result=sqlite3_changes(hdb)
errstr=sql3errMsg(hdb)
err_str=errstr

else
  currow=0
  for i=0 to cols-1
   ptable.cell(i,currow)=varptr$(sqlite3_column_name(stmt,i))
  next i
  do
   rezultat=sqlite3_step(stmt)
   if rezultat= SQLITE3_ERROR or rezultat= SQLITE3_MISUSE then
    result=-rezultat
    errstr=sql3errMsg(hdb)
    err_str=insert$("step error: ",errstr,1)
    exit function
   end if
   inc(currow)
   ptable.insertrow(currow)
   for i=0 to cols-1
    ptable.cell(i,currow)=varptr$(sqlite3_column_text(stmt,i))
   next i
  loop until rezultat=SQLITE3_DONE
result=currow
ptable.fixedrows=1
ptable.colcount=cols
ptable.rowcount=currow
ptable.fixedcols=0
end if
sqlite3_close(hdb)
end function

